
const _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');

exports.__esModule = true;
exports.default = void 0;

const _babelHelperVueJsxMergeProps = _interopRequireDefault(require('@vue/babel-helper-vue-jsx-merge-props'));

const _utils = require('../utils');

const _resetScroll = require('../utils/dom/reset-scroll');

const _event = require('../utils/dom/event');

const _field = require('../mixins/field');

const _utils2 = require('../field/utils');

const _createNamespace = (0, _utils.createNamespace)('stepper');
const createComponent = _createNamespace[0];
const bem = _createNamespace[1];

const LONG_PRESS_START_TIME = 600;
const LONG_PRESS_INTERVAL = 200;

function equal(value1, value2) {
  return String(value1) === String(value2);
} // add num and avoid float number


function add(num1, num2) {
  const cardinal = Math.pow(10, 10);
  return Math.round((num1 + num2) * cardinal) / cardinal;
}

const _default = createComponent({
  mixins: [_field.FieldMixin],
  props: {
    value: null,
    integer: Boolean,
    disabled: Boolean,
    inputWidth: [Number, String],
    buttonSize: [Number, String],
    asyncChange: Boolean,
    disablePlus: Boolean,
    disableMinus: Boolean,
    disableInput: Boolean,
    decimalLength: [Number, String],
    name: {
      type: [Number, String],
      default: '',
    },
    min: {
      type: [Number, String],
      default: 1,
    },
    max: {
      type: [Number, String],
      default: Infinity,
    },
    step: {
      type: [Number, String],
      default: 1,
    },
    defaultValue: {
      type: [Number, String],
      default: 1,
    },
    showPlus: {
      type: Boolean,
      default: true,
    },
    showMinus: {
      type: Boolean,
      default: true,
    },
    longPress: {
      type: Boolean,
      default: true,
    },
  },
  data: function data() {
    const defaultValue = (0, _utils.isDef)(this.value) ? this.value : this.defaultValue;
    const value = this.format(defaultValue);

    if (!equal(value, this.value)) {
      this.$emit('input', value);
    }

    return {
      currentValue: value,
    };
  },
  computed: {
    minusDisabled: function minusDisabled() {
      return this.disabled || this.disableMinus || this.currentValue <= this.min;
    },
    plusDisabled: function plusDisabled() {
      return this.disabled || this.disablePlus || this.currentValue >= this.max;
    },
    inputStyle: function inputStyle() {
      const style = {};

      if (this.inputWidth) {
        style.width = (0, _utils.addUnit)(this.inputWidth);
      }

      if (this.buttonSize) {
        style.height = (0, _utils.addUnit)(this.buttonSize);
      }

      return style;
    },
    buttonStyle: function buttonStyle() {
      if (this.buttonSize) {
        const size = (0, _utils.addUnit)(this.buttonSize);
        return {
          width: size,
          height: size,
        };
      }
    },
  },
  watch: {
    max: 'check',
    min: 'check',
    integer: 'check',
    decimalLength: 'check',
    value: function value(val) {
      if (!equal(val, this.currentValue)) {
        this.currentValue = this.format(val);
      }
    },
    currentValue: function currentValue(val) {
      this.$emit('input', val);
      this.$emit('change', val, {
        name: this.name,
      });
    },
  },
  methods: {
    check: function check() {
      const val = this.format(this.currentValue);

      if (!equal(val, this.currentValue)) {
        this.currentValue = val;
      }
    },
    // formatNumber illegal characters
    formatNumber: function formatNumber(value) {
      return (0, _utils2.formatNumber)(String(value), !this.integer);
    },
    format: function format(value) {
      value = this.formatNumber(value); // format range

      value = value === '' ? 0 : +value;
      value = Math.max(Math.min(this.max, value), this.min); // format decimal

      if ((0, _utils.isDef)(this.decimalLength)) {
        value = value.toFixed(this.decimalLength);
      }

      return value;
    },
    onInput: function onInput(event) {
      const { value } = event.target;
      let formatted = this.formatNumber(value); // limit max decimal length

      if ((0, _utils.isDef)(this.decimalLength) && formatted.indexOf('.') !== -1) {
        const pair = formatted.split('.');
        formatted = `${pair[0]}.${pair[1].slice(0, this.decimalLength)}`;
      }

      if (!equal(value, formatted)) {
        event.target.value = formatted;
      }

      this.emitChange(formatted);
    },
    emitChange: function emitChange(value) {
      if (this.asyncChange) {
        this.$emit('input', value);
        this.$emit('change', value, {
          name: this.name,
        });
      } else {
        this.currentValue = value;
      }
    },
    onChange: function onChange() {
      const { type } = this;

      if (this[`${type}Disabled`]) {
        this.$emit('overlimit', type);
        return;
      }

      const diff = type === 'minus' ? -this.step : +this.step;
      const value = this.format(add(+this.currentValue, diff));
      this.emitChange(value);
      this.$emit(type);
    },
    onFocus: function onFocus(event) {
      this.$emit('focus', event); // readonly not work in lagacy mobile safari

      /* istanbul ignore if */

      if (this.disableInput && this.$refs.input) {
        this.$refs.input.blur();
      }
    },
    onBlur: function onBlur(event) {
      const value = this.format(event.target.value);
      event.target.value = value;
      this.currentValue = value;
      this.$emit('blur', event);
      (0, _resetScroll.resetScroll)();
    },
    longPressStep: function longPressStep() {
      const _this = this;

      this.longPressTimer = setTimeout(() => {
        _this.onChange();

        _this.longPressStep(_this.type);
      }, LONG_PRESS_INTERVAL);
    },
    onTouchStart: function onTouchStart() {
      const _this2 = this;

      if (!this.longPress) {
        return;
      }

      clearTimeout(this.longPressTimer);
      this.isLongPress = false;
      this.longPressTimer = setTimeout(() => {
        _this2.isLongPress = true;

        _this2.onChange();

        _this2.longPressStep();
      }, LONG_PRESS_START_TIME);
    },
    onTouchEnd: function onTouchEnd(event) {
      if (!this.longPress) {
        return;
      }

      clearTimeout(this.longPressTimer);

      if (this.isLongPress) {
        (0, _event.preventDefault)(event);
      }
    },
  },
  render: function render() {
    const _this3 = this;

    const h = arguments[0];

    const createListeners = function createListeners(type) {
      return {
        on: {
          click: function click() {
            _this3.type = type;

            _this3.onChange();
          },
          touchstart: function touchstart() {
            _this3.type = type;

            _this3.onTouchStart();
          },
          touchend: _this3.onTouchEnd,
          touchcancel: _this3.onTouchEnd,
        },
      };
    };

    return h('div', {
      class: bem(),
    }, [h('button', (0, _babelHelperVueJsxMergeProps.default)([{
      directives: [{
        name: 'show',
        value: this.showMinus,
      }],
      attrs: {
        type: 'button',
      },
      style: this.buttonStyle,
      class: bem('minus', {
        disabled: this.minusDisabled,
      }),
    }, createListeners('minus')])), h('input', {
      ref: 'input',
      attrs: {
        type: this.integer ? 'tel' : 'text',
        role: 'spinbutton',
        disabled: this.disabled,
        readonly: this.disableInput,
        inputmode: this.integer ? 'numeric' : 'decimal',
        'aria-valuemax': this.max,
        'aria-valuemin': this.min,
        'aria-valuenow': this.currentValue,
      },
      class: bem('input'),
      domProps: {
        value: this.currentValue,
      },
      style: this.inputStyle,
      on: {
        input: this.onInput,
        focus: this.onFocus,
        blur: this.onBlur,
      },
    }), h('button', (0, _babelHelperVueJsxMergeProps.default)([{
      directives: [{
        name: 'show',
        value: this.showPlus,
      }],
      attrs: {
        type: 'button',
      },
      style: this.buttonStyle,
      class: bem('plus', {
        disabled: this.plusDisabled,
      }),
    }, createListeners('plus')]))]);
  },
});

exports.default = _default;
